package pers.corvey.studyjava.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import pers.corvey.studyjava.exception.InternalServerErrorException;
import pers.corvey.studyjava.model.Chapter;
import pers.corvey.studyjava.model.form.ChapterForm;
import pers.corvey.studyjava.service.ChapterService;

@Controller
@RequestMapping("/courseCategorys/{courseCategoryId}/courses/{courseId}/chapters")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;
    
    @GetMapping(value="/{chapterId}", headers="Accept=text/html")
    public String show() {
        return "forward://html/showChapter.html";
    }
    
    @GetMapping(value="/addChapter", headers="Accept=text/html")
    public String showAdd() {
        return "forward://addChapter.html";
    }
    
    @GetMapping("/{chapterId}/content")
    public String showContent(@PathVariable String chapterId) {
        Chapter chapter = chapterService.getChapterById(chapterId);
        return "forward:/" + chapter.getFilePath();
    }
    
    @GetMapping
    @ResponseBody
    public Iterable<Chapter> getChapters(@PathVariable String courseId) {
        return chapterService.getChapters(courseId);
    }
    
    @GetMapping("/{chapterId}")
    @ResponseBody
    public Chapter getChapter(@PathVariable String chapterId) {
        return chapterService.getChapterById(chapterId);
    }
    
    @PostMapping
    @ResponseBody
    public Chapter createChapter(@PathVariable String courseId, ChapterForm chapterForm) {
        chapterForm.setCourseId(courseId);
        chapterForm.validate();
        Chapter chapter;
        try {
            chapter = chapterForm.toChapter();
        } catch (IOException e) {
            throw new InternalServerErrorException("服务器发生错误！", e);
        }
        return chapterService.createChapter(chapter);
    }
    
    @PutMapping("/{chapterId}")
    @ResponseBody
    public Chapter updateChapter(@PathVariable String chapterId, ChapterForm chapterForm) {
        chapterForm.validate();
        Chapter chapter;
        try {
            chapter = chapterForm.toChapter();
        } catch (IOException e) {
            throw new InternalServerErrorException("服务器发生错误！", e);
        }
        chapter.setId(chapterId);
        return chapterService.updateChapter(chapter);
    }
    
    @DeleteMapping("/{chapterId}")
    @ResponseBody
    public void deleteChapter(@PathVariable String chapterId) {
        chapterService.deleteChapterById(chapterId);
    }
    
}
